package com.eastdigit.system.controller;

import com.eastdigit.servlet.Response;
import com.eastdigit.system.model.Constants;
import com.eastdigit.system.service.ICaptchaService;
import com.eastdigit.system.service.ICodeService;
import com.eastdigit.util.PropertiesUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(value = "")
public class SystemController {

    @Autowired
    private ICodeService codeService;
    @Autowired
    private ICaptchaService captchaService;

    /**
     * 验证码
     */
    @RequestMapping(value = "/captcha", method = RequestMethod.GET)
    public void image(HttpServletRequest request, HttpServletResponse response) throws Exception {
        captchaService.buildImage(request, response);
    }

    @RequestMapping(value = "/system/codes")
    @ResponseBody
    public JSONObject queryCodes() {
        return Response.response(codeService.queryCodes());
    }

    @RequestMapping(value = "/unauthorized", method = RequestMethod.GET)
    public String unauthorized() {
        return "/common/jsp/unauth";
    }

    @RequestMapping(value = "/unauthorized", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject unauthorized2() {
        return Response.responseError("0", "对不起，您没有请求该功能的权限！");
    }

    @RequestMapping(value = "/incorrectCredentials", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject incorrectCredentials() {
        return Response.responseError("2", "您的登录状态已失效，请重新登录！");
    }

    @RequestMapping(value = "/system/setting", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject setting() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("projectName", PropertiesUtil.getProperty("project.name"));
        jsonObject.put("loginCaptcha", Constants.LOGIN_CAPTCHA);
        jsonObject.put("nginxPath", Constants.NGINX);
        return Response.response(jsonObject);
    }

    @RequestMapping(value = "/dynamic/datatable", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject test() {
        JSONArray rows = new JSONArray();

        JSONArray row1 = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("col", "userCode");
        jsonObject.put("name", "用户编码");
        jsonObject.put("rowspan", "2");
        jsonObject.put("width", "200");
        row1.add(jsonObject);
        jsonObject = new JSONObject();
        jsonObject.put("col", "");
        jsonObject.put("name", "colspan");
        jsonObject.put("colspan", "2");
        row1.add(jsonObject);
        rows.add(row1);

        JSONArray row2 = new JSONArray();
        // jsonObject = new JSONObject();
        // jsonObject.put("col", "userCode");
        // jsonObject.put("name", "测试字段一");
        // row2.add(jsonObject);
        jsonObject = new JSONObject();
        jsonObject.put("col", "userName");
        jsonObject.put("name", "用户名称");
        jsonObject.put("edit", "input");
        jsonObject.put("width", "50");
        row2.add(jsonObject);
        jsonObject = new JSONObject();
        jsonObject.put("col", "gender");
        jsonObject.put("name", "性别");
        jsonObject.put("option", "gender");
        jsonObject.put("edit", "select");
        jsonObject.put("width", "50");
        row2.add(jsonObject);
        rows.add(row2);
        return Response.responseSuccess(rows);
    }
}
